package cn.px.service.wechat.handler;

import cn.px.common.util.JaxbUtil;
import cn.px.service.wechat.constant.WechatConstant;
import cn.px.service.wechat.model.message.send.passive.TextResponse;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 *
 * 普通文本消息处理类 处理微信推送的模版消息处理结果
 *
 *
 * @author 品讯科技
 *
 */
@Slf4j
public class TextHandler implements BaseRequestHandler {

	public static final String XML_COLUMN_CONTENT = "Content";

	@Override
	public void handler(Map<String, String> requestMap, HttpServletResponse response) throws IOException {
		log.info("正在处理文件消息");
		String respContent = "您发送的是文本消息！";
		TextResponse text = new TextResponse();
		text.setContent(respContent);
		text.setCreateTime(System.currentTimeMillis());
		// 发送方帐号
		String fromUserName = requestMap.get(XML_COLUMN_FROMUSERNAME);
		// 开发者微信号
		String toUserName = requestMap.get(XML_COLUMN_TOUSERNAME);
		// 消息类型
		text.setToUserName(fromUserName);
		text.setFromUserName(toUserName);
		text.setMsgType(WechatConstant.RESP_MESSAGE_TYPE_TEXT);
		String respXml = JaxbUtil.convertToXmlNoHead(text);
		log.info("返回数据：" + respXml);
		PrintWriter pw = null;
		pw = response.getWriter();
		pw.print(respXml);
		pw.flush();
		pw.close();
	}

}
